
libname hcup2009 '.';
libname sidwork '.';

data states;
input state $2. fips 2. hospid 1.;
cards;
VT500
NC370
NE310
CO080
AR050
UT490
AZ040
MD240
NJ340
NY360
WI550
IA190
OR410
KY211
MI261
HI151
SD461
run;

**Import list of chronic illness diagnosis codes;
**Downloaded from https://www.hcup-us.ahrq.gov/toolssoftware/chronic/chronic.jsp on 8/6/2014;

PROC IMPORT OUT= WORK.CCI 
            DATAFILE= "cci2012.csv" 
            DBMS=CSV REPLACE;
     GETNAMES=YES;
     DATAROW=2; 
RUN;

data cci;
set cci;
DX1=ICD_9_CM_CODE;
run;

proc sort data=cci;
by DX1;
run;

**Import cost to charge ratio file;
**Downloaded from https://www.hcup-us.ahrq.gov/db/state/costtocharge.jsp on 8/6/2014;

PROC IMPORT OUT= WORK.CostChargeRatio 
            DATAFILE= "cc2009SID.csv" 
            DBMS=CSV REPLACE;
     GETNAMES=YES;
     DATAROW=2; 
RUN;

proc sort data=costchargeratio;
by hospid;
run;

data costchargeratio (drop=hospid);
set costchargeratio;
hospid2=hospid*1;
run;

data costchargeratio (drop=hospid2);
set costchargeratio;
hospid=hospid2;
run;


%macro sid2009;

%do i=1 %to 17;

data _null_;
set states;
if _n_=&i then do;
call symput('state', state);
call symput('fips', fips);
call symput('hospid', hospid);
end;

%put &state;
%put &fips;

data sidwork.&state._sid_2009_core;
set hcup2009.&state._sid_2009_core;
FIPS=&fips;
run;

%if &hospid=0 %then %do;

proc sort data=sidwork.&state._sid_2009_core;
by dshospid;
quit;

data sidwork.&state._sid_2009_core;
merge sidwork.&state._sid_2009_core sidwork.&state._sid_2009_ahal;
by dshospid;
run;

%end;

%if &i=1 %then %do;

data sidwork.sid2009;
set sidwork.&state._sid_2009_core;
run;
%end;

%else %do;

data sidwork.sid2009;
set sidwork.sid2009 sidwork.&state._sid_2009_core;
run;

%end;

x rm -f sidwork.&state._sid_2009_core;

%end;

data sidwork.sid2009;
set sidwork.sid2009;
count=1;
bdate=trim(left(bmonth))||trim(left(byear));
SEX=FEMALE-1;

***define payors;

if pay1 LT 3 then public=1;
else public=0;
if pay1=3 then private=1;
else private=0;
if pay1=4 then self=1;
else self=0;
if pay1=5 then freecare=1;
else freecare=0;

**define diagnosis categories, from https://en.wikipedia.org/wiki/List_of_ICD-9_codes accessed 7/15/2015;

ThreeDigDx2=substr(trim(left(dx1)),1,1);

if ThreeDigDx2="E" then ecode=1;
else ecode=0;
if ThreeDigDx2="V" then ThreeDigDx=substr(DX2,1,3)*1;

else ThreeDigDx=substr(DX1,1,3)*1;
if ThreeDigDx LE 679 and ThreeDigDx GE 630 then preg=1;
else preg=0;

if threeDigDx LE 319 and ThreeDigDx GE 290 then mental=1;
else mental=0;
if threeDigDX GE 800 and threeDigDX LT 830 then fracture=1;
else fracture=0;
if threeDigDX GE 800 and threeDigDX LT 960 then injury=1;
else injury=0;
if threeDigDX=. then fracture=.;
if threeDigDX=. then mental=.;
if threeDigDX=. then injury=.;


if ThreeDigDx=540 then Appendicitis=1;
else Appendicitis=0;

if threedigdx GT 0 and threedigdx LE 139 then infect=1;
else infect=0;
if threedigdx GT 139 and threedigdx LE 239 then neoplasms=1;
else neoplasms=0;
if threedigdx GT 239 and threedigdx LE 279 then endometa=1;
else endometa=0;
if threedigdx GT 279 and threedigdx LE 289 then blood=1;
else blood=0;
if threedigdx GT 289 and threedigdx LE 319 then mentaldisorders=1;
else mentaldisorders=0;
if threedigdx GT 319 and threedigdx LE 359 then nervoussys=1;
else nervoussys=0;
if threedigdx GT 359 and threedigdx LE 389 then sense=1;
else sense=0;
if threedigdx GT 389 and threedigdx LE 459 then circulatory=1;
else circulatory=0;
if threedigdx GT 459 and threedigdx LE 519 then respiratory=1;
else respiratory=0;
if threedigdx GT 519 and threedigdx LE 579 then digestive=1;
else digestive=0;
if threedigdx GT 579 and threedigdx LE 629 then genitourinary=1;
else genitourinary=0;
if threedigdx GT 679 and threedigdx LE 709 then skin=1;
else skin=0;
if threedigdx GT 709 and threedigdx LE 739 then muscle=1;
else muscle=0;
if threedigdx GT 739 and threedigdx LE 759 then congenital=1;
else congenital=0;
if threedigdx GT 759 and threedigdx LE 779 then perinatal=1;
else perinatal=0;
if threedigdx GT 779 and threedigdx LE 799 then illdefined=1;
else illdefined=0;
if threedigdx GT 799 and threedigdx LE 999 then injpois=1;
else injpois=0;

*exclude hospitalizations related to pregnancy and delivery;

if preg=1 then delete;
run;

**Merge with chronic illness indicator;

proc sort data=sidwork.sid2009;
by DX1;
run;

data sidwork.sid2009;
merge sidwork.sid2009 cci;
by DX1;
if count=. then delete;
Chronic=CATEGORY_DESCRIPTION*1;
run;

**Merge with cost to charge ratio;

proc sort data=sidwork.sid2009;
by hospid;
quit;

data sidwork.sid2009;
merge sidwork.sid2009 costchargeratio;
by hospid;
if hospid=. then delete;
cost=TOTCHG*APICC;
run;

proc print data=sidwork.sid2009 (Obs=50);
quit;

proc means data=sidwork.sid2009 N NMISS MIN MAX MEAN SUM;
quit;

proc sort data=sidwork.sid2009;
by fips;
quit;

proc means data=sidwork.sid2009 N NMISS MIN MAX MEAN SUM;
by fips;
quit;

%include addup2009_restat.sas;

%mend;

%sid2009;
